import { request } from "@/utils/http";
import type { UploadImage, UserData } from '@/types/user'

export type CodeType = 'login' | 'register' | 'changeMobile' | 'forgetPassword' | 'bindMobile'
export interface GetCodeParams {
  mobile: string;
  type: CodeType;
}
export type GetCodeData = {
  code: string
}

export interface LoginCodeParams {
  /**
   * 验证码
   */
  code: string;
  /**
   * 手机号
   */
  mobile: string;
}

export interface EmptyObject {
  /**
   * 手机号
   */
  mobile: string
  /**
   * 密码
   */
  password: string
}

export interface LoginCodeData {
  /**
   * 用户名
   */
  account?: string
  /**
   * 头像
   */
  avatar?: string
  /**
   * 用户id
   */
  id: string
  /**
   * 脱敏手机号，带星号的手机号
   */
  mobile?: string
  /**
   * refreshToken
   */
  refreshToken: string
  /**
   * token
   */
  token: string
}
export const getCodeApi = (params: GetCodeParams) => {
  return request.get<null, GetCodeData>('/code', {
    params: params,
  })
}

/** 登录--手机验证码登录 */
export const loginByMobileApi = (data: LoginCodeParams) => {
  return request.post<null, LoginCodeData>('/login', data)
}

/** 登录--密码登录 */
export const loginByPasswordApi = (data: EmptyObject) => {
  return request.post<null, LoginCodeData>('/login/password', data)
}

//获取个人信息
export const userInfoApi = () => {
  return request.get<null, UserData>('/patient/myUser')
}

//上传图片
export const uploadImageApi = (filePath: string) => {
  return request.upload<null, UploadImage>('/upload', {
    name: 'file', // 服务器的 key
    filePath: filePath, // 上传文件资源的路径(沙箱路径)，上传时会自动转换成二进制
  })
}